﻿using System;
using System.Collections.Generic;
using FrankJie.Model;
using FrankJie.IDAL;
using FrankJie.Cache;

namespace FrankJie.BLL
{
    public class Category
    {
        private readonly static ICategory dal = DataAccess.CreateCategory();

        /// <summary>
        /// 获取分类列表
        /// </summary>
        /// <returns>返回分类集合</returns>
        public static List<CategoryInfo> GetList()
        {
            List<CategoryInfo> categoryInfoList = new List<CategoryInfo>();
            categoryInfoList = (List<CategoryInfo>)CacheStrategy.GetObject(CacheKey.CATEGORY_LIST);
            if (categoryInfoList == null)
            {
                categoryInfoList = dal.GetList();
                CacheStrategy.AddObject(CacheKey.CATEGORY_LIST, categoryInfoList);
            }
            return categoryInfoList;
        }

        /// <summary>
        /// 增加分类所拥有的数量
        /// </summary>
        /// <param name="pid">ID</param>
        /// <param name="objectType">类别 枚举ObjectType</param>
        /// <returns>返回是否成功</returns>
        public static bool AddCount(int id, int objectType)
        {
            bool isSuccess = dal.AddCount(id, objectType);
            if (isSuccess)
            {
                CacheStrategy.RemoveObject(CacheKey.CATEGORY_LIST);
            }
            return isSuccess;
        }

        /// <summary>
		/// 增加一个分类
		/// </summary>
		/// <param name="model">分类实体</param>
		/// <returns>返回分类ID</returns>
        public static int Add(CategoryInfo categoryInfo)
        {
            int id = dal.Add(categoryInfo);
            if (id > 0)
            {
                CacheStrategy.RemoveObject(CacheKey.CATEGORY_LIST);
            }
            return id;
        }

        /// <summary>
        /// 更新一个分类
        /// </summary>
        /// <param name="categoryInfo">分类实体</param>
        /// <returns>返回是否成功</returns>
        public static bool Update(CategoryInfo categoryInfo)
        {
            bool isSuccess = dal.Update(categoryInfo);
            if (isSuccess)
            {
                CacheStrategy.RemoveObject(CacheKey.CATEGORY_LIST);
            }
            return isSuccess;
        }

        /// <summary>
        /// 删除一个分类
		/// </summary>
		/// <param name="Id">分类ID</param>
		/// <returns>返回是否成功</returns>
        public static bool Delete(int id)
        {
            bool isSuccess = dal.Delete(id);
            if (isSuccess)
            {
                CacheStrategy.RemoveObject(CacheKey.CATEGORY_LIST);
            }
            return isSuccess;
        }
    }
}
